﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ArchPostprocessing.CompoundStructureManage.DataDefinition;
using ArchPostprocessing.CompoundStructureManage.Managers;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKArch.Utils;
using YJKArch.Utils.SelectMaterial;

namespace ArchPostprocessing.CompoundStructureManage.Forms
{
	public partial class CompoundStructureCommonForm : System.Windows.Forms.Form
	{
		public CompoundStructureCommonForm(IntPtr parentHandle, Autodesk.Revit.DB.Document doc, List<WallLayerInfos> wallTypeLayerInfos)
		{
			this.m_ParentHandle = parentHandle;
			this.m_Doc = doc;
			this.m_CurrentWallLayerInfo = wallTypeLayerInfos;
			this.InitializeComponent();
		}

		public List<WallLayerInfos> CurrentWallLayerInfo
		{
			get
			{
				return this.m_CurrentWallLayerInfo;
			}
			set
			{
				this.m_CurrentWallLayerInfo = value;
				this.UpdateCurrentLayerStructInfoGridView(0);
			}
		}

		public void SetButtonsEnable(bool enabled)
		{
			this.btnAddLayer.Enabled = enabled;
			this.btnDeleteLayer.Enabled = enabled;
			this.btnMoveDown.Enabled = enabled;
			this.btnMoveUp.Enabled = enabled;
		}

		public void SetGroupBoxText(string text)
		{
			this.groupBoxMain.Text = text;
		}

		public new void Update()
		{
			foreach (WallLayerInfos wallLayerInfos in this.m_CurrentWallLayerInfo)
			{
				wallLayerInfos.Update(this.m_Doc);
			}
		}

		[DllImport("User32.dll")]
		private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void CompoundStructureSettingForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.UpdateCurrentLayerStructInfoGridView(0);
		}

		private void CompoundStructureSettingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void btnMoveUp_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			if (rowIndex == 0)
			{
				return;
			}
			if (!this.IsValiditySwap(this.m_CurrentWallLayerInfo[rowIndex - 1], this.m_CurrentWallLayerInfo[rowIndex]))
			{
				return;
			}
			this.m_CurrentWallLayerInfo.Insert(rowIndex - 1, this.m_CurrentWallLayerInfo[rowIndex]);
			this.m_CurrentWallLayerInfo.RemoveAt(rowIndex + 1);
			this.UpdateCurrentLayerStructInfoGridView(rowIndex - 1);
			this.cmbboxLayerType.Visible = false;
			CompoundStructureCommonForm.SendMessage(this.m_ParentHandle, 1125, IntPtr.Zero, IntPtr.Zero);
		}

		private void btnMoveDown_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			if (rowIndex == this.dbgLayerInfoList.RowCount - 1)
			{
				return;
			}
			if (!this.IsValiditySwap(this.m_CurrentWallLayerInfo[rowIndex], this.m_CurrentWallLayerInfo[rowIndex + 1]))
			{
				return;
			}
			this.m_CurrentWallLayerInfo.Insert(rowIndex + 2, this.m_CurrentWallLayerInfo[rowIndex]);
			this.m_CurrentWallLayerInfo.RemoveAt(rowIndex);
			this.UpdateCurrentLayerStructInfoGridView(rowIndex + 1);
			this.cmbboxLayerType.Visible = false;
			CompoundStructureCommonForm.SendMessage(this.m_ParentHandle, 1125, IntPtr.Zero, IntPtr.Zero);
		}

		private void btnAddLayer_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			WallLayerInfos wallLayerInfos = this.m_CurrentWallLayerInfo[rowIndex];
			if (wallLayerInfos.StructureLayer == null)
			{
				MessageBox.Show("不能通过核心边界创建新层", "盈建科提示");
				return;
			}
			WallLayerInfos item = new WallLayerInfos(new CompoundStructureLayer(wallLayerInfos.StructureLayer), wallLayerInfos.Name, wallLayerInfos.MaterialName, wallLayerInfos.Thickness);
			if (rowIndex == this.dbgLayerInfoList.RowCount - 1)
			{
				this.m_CurrentWallLayerInfo.Add(item);
			}
			else
			{
				this.m_CurrentWallLayerInfo.Insert(rowIndex + 1, item);
			}
			this.UpdateCurrentLayerStructInfoGridView(rowIndex);
			this.cmbboxLayerType.Visible = false;
			CompoundStructureCommonForm.SendMessage(this.m_ParentHandle, 1125, IntPtr.Zero, IntPtr.Zero);
		}

		private void btnDeleteLayer_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			if (this.m_CurrentWallLayerInfo.Count < 2)
			{
				MessageBox.Show("必须保留至少一项", "盈建科提示");
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			WallLayerInfos wallLayerInfos = this.m_CurrentWallLayerInfo[rowIndex];
			if (wallLayerInfos.StructureLayer == null)
			{
				MessageBox.Show("核心边界不能删除", "盈建科提示");
				return;
			}
			if ((int)wallLayerInfos.StructureLayer.Function == 1)
			{
				int num = 0;
				foreach (WallLayerInfos wallLayerInfos2 in this.m_CurrentWallLayerInfo)
				{
					if (wallLayerInfos2.StructureLayer != null && (int)wallLayerInfos2.StructureLayer.Function == 1)
					{
						num++;
					}
				}
				if (num == 1)
				{
					MessageBox.Show("必须保留一个结构层", "盈建科提示");
					return;
				}
			}
			this.m_CurrentWallLayerInfo.RemoveAt(rowIndex);
			this.UpdateCurrentLayerStructInfoGridView(rowIndex);
			this.cmbboxLayerType.Visible = false;
			CompoundStructureCommonForm.SendMessage(this.m_ParentHandle, 1125, IntPtr.Zero, IntPtr.Zero);
		}

		private void UpdateCurrentLayerStructInfoGridView(int seletedRowIndex)
		{
			this.dbgLayerInfoList.Rows.Clear();
			foreach (WallLayerInfos wallLayerInfos in this.m_CurrentWallLayerInfo)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = wallLayerInfos.Name;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewButtonCell dataGridViewButtonCell;
				if (wallLayerInfos.StructureLayer == null)
				{
					dataGridViewButtonCell = new DataGridViewDisableButtonCell();
					((DataGridViewDisableButtonCell)dataGridViewButtonCell).Enabled = false;
				}
				else
				{
					dataGridViewButtonCell = new DataGridViewButtonCell();
				}
				dataGridViewButtonCell.Value = wallLayerInfos.MaterialName;
				dataGridViewRow.Cells.Add(dataGridViewButtonCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = wallLayerInfos.Thickness;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				if (wallLayerInfos.StructureLayer == null)
				{
					dataGridViewRow.ReadOnly = true;
					dataGridViewRow.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
				}
				this.dbgLayerInfoList.Rows.Add(dataGridViewRow);
			}
			if (seletedRowIndex > -1 && seletedRowIndex < this.dbgLayerInfoList.RowCount)
			{
				this.dbgLayerInfoList.Rows[seletedRowIndex].Cells[0].Selected = true;
			}
		}

		private bool IsValiditySwap(WallLayerInfos exteriorLayer, WallLayerInfos interiorLayer)
		{
			return true;
		}

		private void dbgLayerInfoList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			this.ClickMaterialColumn(sender, e);
			this.ClickLayerColumn(sender, e);
			CompoundStructureCommonForm.SendMessage(this.m_ParentHandle, 1125, IntPtr.Zero, IntPtr.Zero);
		}

		private void ClickMaterialColumn(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != 1 || e.RowIndex == -1)
			{
				return;
			}
			string text = this.dbgLayerInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
			if (text.IndexOf("包络") != -1)
			{
				return;
			}
			Transaction transaction = new Transaction(this.m_Doc);
			transaction.Start("change info");
			try
			{
				string text2 = "";
				if (!MaterialSelectManager.MaterialSelect(this.m_Doc, text, ref text2))
				{
					if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
					{
						transaction.RollBack();
					}
				}
				else
				{
					this.dbgLayerInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = text2;
					this.m_CurrentWallLayerInfo[e.RowIndex].MaterialName = text2;
					CompoundStructureLayer compoundStructureLayer = new CompoundStructureLayer(this.m_CurrentWallLayerInfo[e.RowIndex].StructureLayer);
					compoundStructureLayer.MaterialId = WallTypeManager.GetMaterialIdByName(text2, this.m_Doc);
					this.m_CurrentWallLayerInfo[e.RowIndex].StructureLayer = compoundStructureLayer;
					transaction.Commit();
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
			}
		}

		private void ClickLayerColumn(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != 0 || e.RowIndex == -1)
			{
				return;
			}
			DataGridViewCell dataGridViewCell = this.dbgLayerInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex];
			if (dataGridViewCell.Value.ToString().IndexOf("核心边界") != -1)
			{
				return;
			}
			int x = this.dbgLayerInfoList.Location.X;
			int y = this.dbgLayerInfoList.Location.Y;
			System.Drawing.Rectangle cellDisplayRectangle = this.dbgLayerInfoList.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
			int x2 = cellDisplayRectangle.X;
			int y2 = cellDisplayRectangle.Y;
			int x3 = x + x2;
			int y3 = y + y2;
			this.cmbboxLayerType.Location = new System.Drawing.Point(x3, y3);
			this.cmbboxLayerType.Size = cellDisplayRectangle.Size;
			int selectedIndex = this.cmbboxLayerType.FindString(dataGridViewCell.Value.ToString());
			this.cmbboxLayerType.SelectedIndex = selectedIndex;
			this.cmbboxLayerType.Visible = true;
			this.cmbboxLayerType.BringToFront();
		}

		private void dbgLayerInfoList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
			if (textBox != null)
			{
				textBox.TextChanged += this.DGVTextBoxTextChanged;
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int index = 0;
			int num = 2;
			if (this.dbgLayerInfoList.CurrentCell.ColumnIndex == num)
			{
				int rowIndex = this.dbgLayerInfoList.CurrentCell.RowIndex;
				string text = ((System.Windows.Forms.TextBox)sender).Text;
				string thickness = this.m_CurrentWallLayerInfo[rowIndex].Thickness;
				int num2 = 0;
				try
				{
					num2 = Convert.ToInt32(text);
				}
				catch (Exception)
				{
					((System.Windows.Forms.TextBox)sender).Text = thickness;
					return;
				}
				string text2 = this.dbgLayerInfoList.Rows[rowIndex].Cells[index].Value.ToString();
				if (text2.CompareTo("涂膜层") == 0)
				{
					if (num2 != 0)
					{
						MessageBox.Show(text2 + "层厚度只能等于零", "盈建科提示");
						((System.Windows.Forms.TextBox)sender).Text = "0";
						return;
					}
				}
				else if (num2 <= 0)
				{
					MessageBox.Show(text2 + "层厚度不能小于等于零", "盈建科提示");
					((System.Windows.Forms.TextBox)sender).Text = thickness;
					return;
				}
				this.m_CurrentWallLayerInfo[rowIndex].Thickness = num2.ToString();
				CompoundStructureLayer compoundStructureLayer = new CompoundStructureLayer(this.m_CurrentWallLayerInfo[rowIndex].StructureLayer);
				compoundStructureLayer.Width = Common.MMToFeet((double)num2);
				this.m_CurrentWallLayerInfo[rowIndex].StructureLayer = compoundStructureLayer;
				CompoundStructureCommonForm.SendMessage(this.m_ParentHandle, 1125, IntPtr.Zero, IntPtr.Zero);
			}
		}

		private void cmbboxLayerType_SelectedIndexChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dbgLayerInfoList.CurrentCell.RowIndex;
			WallLayerInfos wallLayerInfos = this.m_CurrentWallLayerInfo[rowIndex];
			if (wallLayerInfos.StructureLayer == null)
			{
				this.cmbboxLayerType.Visible = false;
				return;
			}
			string text = this.cmbboxLayerType.Items[this.cmbboxLayerType.SelectedIndex].ToString();
			if (text.CompareTo(wallLayerInfos.Name) == 0)
			{
				this.cmbboxLayerType.Visible = false;
				return;
			}
			this.cmbboxLayerType.Visible = false;
			this.m_CurrentWallLayerInfo[rowIndex].Name = text;
            MaterialFunctionAssignment function = (MaterialFunctionAssignment)1;
			if (!(text == "衬底"))
			{
				if (!(text == "保温层/空气层"))
				{
					if (!(text == "面层1"))
					{
						if (!(text == "面层2"))
						{
							if (text == "涂膜层")
							{
                                function = (MaterialFunctionAssignment)100;
							}
						}
						else
						{
                            function = (MaterialFunctionAssignment)5;
						}
					}
					else
					{
                        function = (MaterialFunctionAssignment)4;
					}
				}
				else
				{
                    function = (MaterialFunctionAssignment)3;
				}
			}
			else
			{
                function = (MaterialFunctionAssignment)2;
			}
			CompoundStructureLayer compoundStructureLayer = new CompoundStructureLayer(this.m_CurrentWallLayerInfo[rowIndex].StructureLayer);
			compoundStructureLayer.Function = function;
			this.m_CurrentWallLayerInfo[rowIndex].StructureLayer = compoundStructureLayer;
			this.UpdateCurrentLayerStructInfoGridView(rowIndex);
		}

		private Document m_Doc;

		private List<WallLayerInfos> m_CurrentWallLayerInfo = new List<WallLayerInfos>();

		public const int USER = 1024;

		public const int WM_UPDATE_TYPE_NAME = 1125;

		private IntPtr m_ParentHandle = IntPtr.Zero;
	}
}
